package com.zengqingfa.algorithm.project;

import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @fileName: GetSumIndexTest
 * @author: zengqingfa
 * @date: 2022/12/29 8:53
 * @description:
 */
public class GetSumIndexTest2 {


    public static void main(String[] args) {
        //请求参数
        List<ItemReqDTO> reqDTOList = new ArrayList<>();
        reqDTOList.add(new ItemReqDTO(1, "A", 3));
        reqDTOList.add(new ItemReqDTO(2, "A", 7));
        reqDTOList.add(new ItemReqDTO(3, "A", 8));
        reqDTOList.add(new ItemReqDTO(4, "A", 5));
        reqDTOList.add(new ItemReqDTO(5, "A", 2));
        reqDTOList.add(new ItemReqDTO(6, "A", 2));
        reqDTOList.add(new ItemReqDTO(7, "A", 7));
        Integer total1 = reqDTOList.stream().map(ItemReqDTO::getNumber).collect(Collectors.summingInt(m -> m));

        //处理库存返回的参数
        List<StockDealRespDTO> respDTOList = new ArrayList<>();
        respDTOList.add(new StockDealRespDTO("A", 3, "S1"));
        respDTOList.add(new StockDealRespDTO("A", 5, "S2"));
        respDTOList.add(new StockDealRespDTO("A", 5, "S3"));
        respDTOList.add(new StockDealRespDTO("A", 3, "S4"));
        respDTOList.add(new StockDealRespDTO("A", 3, "S5"));
        respDTOList.add(new StockDealRespDTO("A", 4, "S6"));
        respDTOList.add(new StockDealRespDTO("A", 1, "S7"));
        respDTOList.add(new StockDealRespDTO("A", 1, "S8"));
        respDTOList.add(new StockDealRespDTO("A", 1, "S9"));
        respDTOList.add(new StockDealRespDTO("A", 1, "S10"));
        respDTOList.add(new StockDealRespDTO("A", 7, "S11"));
        Integer total2 = respDTOList.stream().map(StockDealRespDTO::getNumber).collect(Collectors.summingInt(m -> m));

        //处理匹配返回对应的结果：需要将对应的行号返回
        List<ItemRespDTO> respList = new ArrayList<>();

        for (int i = 0; i < reqDTOList.size(); i++) {
            ItemReqDTO itemReqDTO = reqDTOList.get(i);
            Integer number = itemReqDTO.getNumber();
            List<StockDealRespDTO> index = getIndex(respDTOList, number);
            //删除数组中元素
            for (int j = 0; j < index.size(); j++) {
                StockDealRespDTO stockDealRespDTO = index.get(j);
                ItemRespDTO itemRespDTO = new ItemRespDTO(itemReqDTO.getRowNo(), itemReqDTO.getMaterialCode(), stockDealRespDTO.getNumber(), stockDealRespDTO.getSerialNo());
                respList.add(itemRespDTO);
                respDTOList.remove(stockDealRespDTO);
            }
        }
        respList.sort(Comparator.comparing(ItemRespDTO::getRowNo));
        respList.stream().forEach(item -> System.out.println(item));
    }


    static List<StockDealRespDTO> getIndex(List<StockDealRespDTO> stockDealRespDTOS, Integer num) {
        Stack<StockDealRespDTO> set = new Stack<>();
        List<StockDealRespDTO> list = Lists.newArrayList();
        for (int i = 0; i < stockDealRespDTOS.size(); i++) {
            StockDealRespDTO stockDealRespDTO = stockDealRespDTOS.get(i);
            Integer number = stockDealRespDTO.getNumber();
            if (number.equals(num)) {
                list.add(stockDealRespDTO);
                break;
            } else if (number > num) {
                continue;
            } else if (number < num) {
                set.push(stockDealRespDTO);
                Integer rest = number - num;
            }
        }
        return list;
    }

    //请求参数
    @Data
    @AllArgsConstructor
    public static class ItemReqDTO {
        private Integer rowNo;
        private String materialCode;
        private Integer number;
    }

    //处理库存返回参数
    @Data
    @AllArgsConstructor
    public static class StockDealRespDTO {
        private String materialCode;
        private Integer number;
        private String serialNo;
    }

    //返回参数
    @Data
    @AllArgsConstructor
    public static class ItemRespDTO {
        private Integer rowNo;
        private String materialCode;
        private Integer number;
        private String serialNo;
    }


}
